<?php

namespace App\Http\Controllers\Admin\RunTest;

use App\Exceptions\LogicException;
use App\Http\Controllers\Controller;
use App\Models\Adminer\Admins;
use App\Models\TestApi\TestReport;
use App\Utils\QueryUtil;
use App\Utils\ResUtil;
use App\Utils\DateUtil;
use App\Services\makeReportService;
use App\Services\RunTestService;



class RunTestController extends Controller
{

    const ID_TYPE_NULL = 0;
    const ID_TYPE_PRODUCT = 1;
    const IDS_TYPE_FMODULE = 2;
    const IDS_TYPE_SMODULE = 3;

    /**
     * -----生成测试单
     * @return array
     * @throws LogicException
     */
    public function makeReport(makeReportService $makeReportService)
    {
        $data = request()->only([
            'remark',
            'product_id', 'f_module_ids', 's_module_ids'
        ]);
        $rule = [
            'remark' => 'string|required',
            'product_id' => 'integer|nullable',
            'f_module_ids' => 'array|nullable',
            's_module_ids' => 'array|nullable',
        ];
        $validator = validatorService()->checkValidator($rule, $data);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }

        $adminer = Admins::getInfoById($this->authAdmin()->id);
        $testReport = TestReport::create([
            'test_report_uid' => TestReport::makeUuid(), // 测试报告唯一UUID
            'title' => $adminer.'于'.DateUtil::now().'创建的测试单',
            'test_api_sun' => 0,
            'test_param_sun' => 0,
            'success_param_sun' => 0,
            'duration' => 0,
            'test_adminer' => '',
            'created_adminer' => $adminer,
            'remark' => $data['remark'],
            'explain' => '',
            'tested_at' => null,
            'status' => TestReport::STATUS_INIT
        ]);
        $id_type = self::ID_TYPE_NULL;
        $product_id = $data['product_id'] ?? 0;
        if($product_id) {
            $id_type = self::ID_TYPE_PRODUCT;
        }

        $f_module_ids = $data['f_module_ids'] ?? [];
        if(!empty($f_module_ids)) {
            $id_type = self::IDS_TYPE_FMODULE;;
        }

        $s_module_ids = $data['s_module_ids'] ?? [];
        if(!empty($s_module_ids)) {
            $id_type = self::IDS_TYPE_SMODULE;;
        }

        $makeReportService->initData($testReport, $id_type, (int)$product_id, $f_module_ids, $s_module_ids)
                ->makeTestModules()
                ->makeTestApis()
                ->makeTestParams()
                ->makeTestvalidators()
                ->done();
        return $this->ok();
    }

    public function runTest(RunTestService $RunTestService)
    {
        $data = request()->only([
            'test_report_uid'
        ]);
        $rule = [
            'test_report_uid' => 'string|required',
        ];
        $validator = validatorService()->checkValidator($rule, $data);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }
        $testReport = TestReport::getByUid($data['test_report_uid']);
        $RunTestService->initData($testReport)
            ->initServe()
            ->runParams()
            ->done();

        $RunTestService->testReport->test_adminer = Admins::getInfoById($this->authAdmin()->id);
        $RunTestService->testReport->tested_at = DateUtil::now();
        $RunTestService->testReport->save();
        return $this->ok();
    }

}
